﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using System.Diagnostics;
    using ConnectBasic.Execution;
    using DlrExpression = Microsoft.Linq.Expressions.Expression;

    /// <summary>
    /// Represents a variable.
    /// </summary>
    [DebuggerDisplay("Var {Name}: {Type.Name}")]
    public class VariableDeclarationStatement : StatementNodeBase
    {
        private readonly string mName;
        private readonly UnboundType mType;

        /// <summary>
        /// Initializes a new variant instance of the <see cref="VariableDeclarationStatement"/> class.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        public VariableDeclarationStatement(string name)
            : this(name, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VariableDeclarationStatement"/> class.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        /// <param name="type">The type of the variable.</param>
        public VariableDeclarationStatement(string name, UnboundType type)
        {
            mName = name;
            mType = type ?? UnboundType.Variant;
        }

        /// <summary>
        /// Gets the name of the variable.
        /// </summary>
        public string Name
        {
            get { return mName; }
        }

        /// <summary>
        /// Gets the type of the variable.
        /// </summary>
        public UnboundType Type
        {
            get { return mType; }
        }

        /// <inheritdoc />
        public override DlrExpression Compile(BasicCodeGenerator gen)
        {
            gen.Scope.AddMethodLocal(DlrExpression.Parameter(gen.ResolveType(mType), mName));

            // Nothing to return
            return DlrExpression.Empty();
        }
    }
}
